<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model.trim="form.title" />
      </el-form-item>
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="form.date"
          placeholder="选择日期时间"
          type="date"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="链接地址" prop="link">
        <el-input v-model.trim="form.link" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.trim="form.sort" type="number" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
  import { doEdit } from '@/api/dynamic'
  import { baseURL } from '@/config'

  export default defineComponent({
    name: 'ZanzhuEdit',
    emits: ['fetch-data'],
    // 处于生命周期函数 beforeCreate 和 Created 两个钩子函数之前的函数
    // 执行 setup 时，组件实例尚未被创建（在 setup() 内部，this 不会是该活跃实例的引用，即不指向vue实例，Vue 为了避免我们错误的使用，直接将 setup函数中的this修改成了 undefined）
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()
      console.log(window.location.host)
      const state = reactive({
        formRef: null,
        uploadActionUrl: baseURL + 'upload/ajaxUploads',
        form: {
          id: '',
          title: '',
          link: '',
          date: '',
          sort: '',
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
          date: [{ required: true, trigger: 'change', message: '请选择日期' }],
          link: [
            { required: true, trigger: 'blur', message: '请输入链接地址' },
            { type: 'url', trigger: 'change', message: '请输入正确的链接地址' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        roleList: [],
      })
      const showEdit = (row) => {
        if (row.id) {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        } else {
          state.title = '添加'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      const handleSuccess = (response) => {
        if (response.code == 0) {
          state.form.logo = response.data[0].path
        }
      }
      const dateChange = (val) => {
        console.log(val)
      }

      // 与模板一起使用：需要返回一个对象 (在setup函数中定义的变量和方法最后都是需要 return 出去的 不然无法再模板中使用)
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        handleSuccess,
        dateChange,
      }
    },
  })
</script>
<style lang="scss" scoped>
  .tip {
    margin: 0px;
    color: red;
  }
  :deep() {
    .el-upload-list {
      display: none;
    }
  }
</style>
